using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Termites
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Texture2D termite;
        private Texture2D goal;
        private BoundingSphere baseBoundingSphere;
        private SpriteFont font;

        List<Termite> termites;
        private Vector2 basePosition;
        private List<Level> levels;
        private Level currentLevel;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            termites = new List<Termite>();
            basePosition = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);
            levels = new List<Level>();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            termite = Content.Load<Texture2D>("Termite");
            goal = Content.Load<Texture2D>("Base");
            font = Content.Load<SpriteFont>("SpriteFont1");

            //Termite t = new Termite(true, termite, 1f);
            //t.Position = new Vector2(30, 20);
            //termites.Add(t);

            baseBoundingSphere = new BoundingSphere(new Vector3(basePosition.X + (goal.Width / 2), basePosition.Y + (goal.Height / 2), 0)
                , goal.Width / 2);
            
            levels.Add(new Level1(baseBoundingSphere, termite, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));
            currentLevel = levels[0];
            currentLevel.Player.Position = new Vector2(30, 20);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            updateTermites(gameTime);
            if(currentLevel.isPlayerInGoal())
            {
                System.Diagnostics.Debug.WriteLine("fdjsla");
                //switch level
            }

            base.Update(gameTime);
        }

        private void updateTermites(GameTime gameTime)
        {
            //termites[0].update(gameTime);
            currentLevel.Player.update(gameTime);
            
            foreach (Termite computerTermite in currentLevel.ComputerTermites)
            {
                computerTermite.update(gameTime);
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            
            spriteBatch.Draw(goal, basePosition, 
                null, Color.Gold, 0, new Vector2(goal.Width / 2, goal.Height / 2),
                        1.0f, SpriteEffects.None, 1f);


            spriteBatch.Draw(termite, currentLevel.Player.Position, null,
                        Color.White, currentLevel.Player.Direction, new Vector2(termite.Width / 2, termite.Height / 2),
                        currentLevel.TermitScale, SpriteEffects.None, 0f);

            foreach (Termite computerTermite in currentLevel.ComputerTermites)
            {
                spriteBatch.Draw(termite, computerTermite.Position, null,
                        Color.White, computerTermite.Direction, new Vector2(termite.Width / 2, termite.Height / 2),
                        currentLevel.TermitScale, SpriteEffects.None, 0f);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
